import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName LC6
 * Z字形变换
 * @Author ykx
 * @Date 2024/3/2 14:31
 * @Version 1.0
 */
public class LC6 {
    public static void main(String[] args) {
        System.out.println(convert("PAYPALISHIRING", 3));
    }

    public static String convert(String s, int numRows) {
        if (numRows == 1) {
            return s;
        }
        // 声明一个变量拼接结果
        StringBuilder result = new StringBuilder();

        // 存每层字符串的跨度
        Map<Integer, Integer[]> map = new HashMap<>();
        for (int i = 0; i < numRows; i++) {
            map.put(i, new Integer[]{2 * (numRows - 1 - i), 2 * i});
        }

        // 开始拼接字符串
        for (int i = 0; i < numRows; i++) {
            // 第几层，就从第几位开始拼接
            int index = i;

            // 当前层的第几次拼接
            int count = 0;

            while (index < s.length()) {
                if (map.get(i)[count % 2] != 0) {
                    result.append(s.charAt(index));
                }
                index = index + map.get(i)[count % 2];
                count++;
            }
        }

        return result.toString();
    }
}
